package com.xlh.virtualization.job.scheduler;

import com.xlh.service.course.ChapterService;
import com.xlh.virtualization.constant.ContextTypeEnum;
import com.xlh.virtualization.dao.helper.ContainerMapperHelper;
import com.xlh.virtualization.domain.Container;
import com.xlh.virtualization.service.ConfigService;
import com.xlh.virtualization.service.ContainerService;
import com.xlh.virtualization.service.ExperimentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时清理容器
 *
 * @author cheer
 */
@Component
@Slf4j
public class ContainerDeleteJob {

    @Autowired
    private ConfigService configService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private ContainerService containerService;

    @Autowired
    private ExperimentService experimentService;

    @Autowired
    private ContainerMapperHelper containerMapperHelper;

    /**
     * 每天凌晨1点执行删除容器定时任务
     */
    @Scheduled(cron = "0 0 1 * * *")
    public void containerDelete() {
        try {
            log.info("开始执行定时删除容器");

            boolean globalSwitch = configService.getContainerDeletedSwitch();
            boolean trainingSwitch = configService.getTrainingContainerDeletedSwitch();
            // 缓存课程容器销毁开关
            Map<Long, Boolean> courseSwitchMap = new HashMap<>();

            List<Container> containers = containerMapperHelper.listAllContainers();
            for (Container container : containers) {

                if (experimentService.isExperimentPageOpen(container.getUserId(), container.getContextType(), container.getContextId())) {
                    return;
                }

                try {
                    boolean deleteSwitch = globalSwitch;

                    ContextTypeEnum contextTypeEnum = ContextTypeEnum.forCode(container.getContextType());
                    if (contextTypeEnum != null) {
                        if (contextTypeEnum == ContextTypeEnum.TASK) {
                            Long courseId = chapterService.getCourseIdByChapterId(container.getContextId());
                            if (courseId != null) {
                                Boolean courseSwitch = courseSwitchMap.get(courseId);
                                if (courseSwitch == null) {
                                    deleteSwitch = configService.getCourseContainerDeletedSwitch(courseId);
                                    courseSwitchMap.put(courseId, deleteSwitch);
                                } else {
                                    deleteSwitch = courseSwitch;
                                }
                            }
                        } else if (contextTypeEnum == ContextTypeEnum.TRAINING) {
                            deleteSwitch = trainingSwitch;
                        } else if (contextTypeEnum == ContextTypeEnum.COURSE) {
                            Boolean courseSwitch = courseSwitchMap.get(container.getContextId());
                            if (courseSwitch == null) {
                                deleteSwitch = configService.getCourseContainerDeletedSwitch(container.getContextId());
                                courseSwitchMap.put(container.getContextId(), deleteSwitch);
                            } else {
                                deleteSwitch = courseSwitch;
                            }
                        }
                    }

                    if (deleteSwitch) {
                        containerService.deleteContainer(container);
                    }
                } catch (Exception e) {
                    log.error("刪除容器 {} 异常", container.getId(), e);
                }
            }

            log.info("执行定时删除容器完毕");
        } catch (Exception e) {
            log.error("执行定时删除容器异常", e);
        }
    }

}
